﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class Secured_AddExpense : System.Web.UI.Page
{
    SqlConnection conn;
    SqlCommand cmd;
    SqlDataReader dr;
    protected void Page_Load(object sender, EventArgs e)
    {
        txtDate.Focus();
        conn = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["connStr"].ConnectionString);
        if (!Page.IsPostBack)
        {
            LoadCurrentDate();
            if (conn.State != System.Data.ConnectionState.Open)
            {
                try
                {
                    conn.Open();
                    FillYear(DateTime.Today.Year);
                    FillUsers();
                    FillExpenseHeads();
                }
                catch (SqlException ex)
                {
                    ltMsg.Text = ex.Message;
                }
            }
        }
    }
    private void LoadCurrentDate()
    {
        txtDate.Text = DateTime.Today.ToShortDateString();
    }
    private void FillYear(int currentYear)
    {
        cmbYear.Items.Clear();
        while (currentYear <= 2020)
        {
            cmbYear.Items.Add(currentYear.ToString());
            currentYear += 1;
        }
    }
    private void FillUsers()
    {
        cmd = new SqlCommand("Select UserName from HAT_UserAccounts where IsApproved=@IsApproved order by UserName asc", conn);
        cmd.Parameters.Add("@IsApproved", System.Data.SqlDbType.Bit).Value = true;
        try
        {
            dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                cmbPaidBy.Items.Add(dr.GetString(0).ToString());
            }
            dr.Close();
        }
        catch (SqlException ex)
        {
            ltMsg.Text = ex.Message;
        }
    }
    private void FillExpenseHeads()
    {
        cmd = new SqlCommand("Select ExpenseHead from HAT_ExpenseHead order by ExpenseHead asc", conn);
        try
        {
            dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                cmbExpense.Items.Add(dr.GetString(0).ToString());
            }
            dr.Close();
        }
        catch (SqlException ex)
        {
            ltMsg.Text = ex.Message;
        }
    }
    protected void btnAddExpense_Click(object sender, EventArgs e)
    {
        Type cstype = this.GetType();
        cmd = new SqlCommand("Insert Into HAT_Expenses Values(@ExpenseDate, @PaidBy, @ForPeriod, @ExpenseHead, @ExpenseDesc, @ExpenseAmt)", conn);
        cmd.Parameters.Add("@ExpenseDate", System.Data.SqlDbType.DateTime).Value = txtDate.Text.Trim();
        cmd.Parameters.Add("@PaidBy", System.Data.SqlDbType.VarChar).Value = cmbPaidBy.SelectedValue.Trim();
        cmd.Parameters.Add("@ForPeriod", System.Data.SqlDbType.VarChar).Value = "0" + (cmbMonth.SelectedIndex + 1) + "/" + cmbYear.SelectedValue;
        cmd.Parameters.Add("@ExpenseHead", System.Data.SqlDbType.VarChar).Value = cmbExpense.SelectedValue.Trim();
        cmd.Parameters.Add("@ExpenseDesc", System.Data.SqlDbType.VarChar).Value = txtNotes.Text;
        cmd.Parameters.Add("@ExpenseAmt", System.Data.SqlDbType.Money).Value = txtAmount.Text.Trim();
        try
        {
            conn.Open();
            cmd.ExecuteNonQuery();
            Page.ClientScript.RegisterClientScriptBlock(cstype, "ExpenseAdded", "alert('Expense Added Successfully')", true);
            ClearFields();
        }
        catch (SqlException ex)
        {
            ltMsg.Text = ex.Message;
        }
    }
    private void ClearFields()
    {
        txtDate.Text = string.Empty;
        txtNotes.Text = string.Empty;
        txtAmount.Text = string.Empty;
        LoadCurrentDate();
    }
    protected void cmbExpense_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (cmbExpense.SelectedValue.Equals("Misc"))
        {
            ltMsg.Text = "Description Field is Mandatory";
            txtNotes.Focus();
        }
    }
}